package com.think.teaching.controller;

import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.common.utils.Common;
import com.think.teaching.model.ExamTest;
import com.think.teaching.service.impl.ExamTestServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.map.HashedMap;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@RestController
@RequestMapping("/v1")
@ResponseBody
@Api(tags = "考试测评管理接口")
public class ExamTestController {

	private final ExamTestServiceImpl examTestService;

	public ExamTestController(ExamTestServiceImpl examTestService) {
		this.examTestService = examTestService;
	}

	@ApiOperation("添加新的考试")
	@PostMapping("admin/examTest")
	public Result AddNewExamTest(@RequestBody() ExamTest data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			ExamTest examTest = examTestService.AddExamTest(data);
			if (examTest != null) {
				return Result.success(examTest);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}

	@ApiOperation("更新考试")
	@RequestMapping(value = "admin/examTest", method = RequestMethod.PUT)
	public Result UpdateExamTest(@RequestBody() ExamTest data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			ExamTest examTest = examTestService.UpdateExamTest(data);
			if (examTest != null) {
				return Result.success(examTest);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}

	@ApiOperation("删除考试")
	@RequestMapping(value = "admin/examTest/{id}", method = RequestMethod.DELETE)
	public Result DeleteExamTest(@PathVariable("id") Integer id) {
		ExamTest examTest = examTestService.getById(id);
		if (examTest == null) {
			return Result.failure(ResultCode.EXAM_TEST_NOT_EXIST);
		} else {
			if (examTestService.DeleteObjectById(id)) {
				return Result.success();
			}
			return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
		}
	}

	@ApiOperation("用户获取考试详情")
	@GetMapping("/user/examTest/details")
	public Result UserGetExamPaper(@RequestParam() Integer id) {
		Integer userId = Common.GetCurrentAuthor().getUserId();
		if (examTestService.getById(id) == null) {
			return Result.failure(ResultCode.EXAM_TEST_NOT_EXIST);
		}
//		if(courseUserServiceImpl.GetByUserIDAndCourseId(id, userId) == null) {
//			return Result.failure(ResultCode.COURSE_USER_NOT_EXIST);
//		};
		return Result.success(examTestService.GetExamPaperWithQuestions(id));
	}
	
	@ApiOperation("用户提交前端考试答案获取成绩")
	@PostMapping("/user/examTest/answer")
	public Result UserSubmitExam(@RequestBody()Map<Integer, String> data) {
		Map<Integer,Boolean> result = new HashedMap<Integer, Boolean>();
		for (Integer key : data.keySet()) {
			result.put(key, true);
		}
		return Result.success(result);
	}

	@ApiOperation("管理员获取考试详情")
	@RequestMapping(value = "admin/examTest/{id}", method = RequestMethod.GET)
	public Result GetExamTest(@PathVariable("id") Integer id) {
		ExamTest examTest = examTestService.getById(id);
		if (examTest == null) {
			return Result.failure(ResultCode.EXAM_TEST_NOT_EXIST);
		} else {
			return Result.success(examTest);
		}
	}

	@ApiOperation("管理员获取已经完成的考试列表")
	@RequestMapping(value = "admin/examTest/finished/{page}/{count}", method = RequestMethod.GET)
	public Result GetFinishedExamTest(@PathVariable("page") Integer page, @PathVariable("count") Integer count,
			@RequestParam(defaultValue = "-1",required = false) Integer course) {
		return Result.success(examTestService.ListFinishedExamTest(page, count,course));
	}

	@ApiOperation("管理员修改考试状态")
	@RequestMapping(value = "admin/examTest/{id}/{state}", method = RequestMethod.PUT)
	public Result UpdateExamTestState(@PathVariable("id") Integer id, @PathVariable("state") Integer state) {
		ExamTest examTest = examTestService.getById(id);
		if (examTest == null) {
			return Result.failure(ResultCode.EXAM_TEST_NOT_EXIST);
		} else {
			try {
				examTest = examTestService.SetExamTestState(id, state);
				return Result.success(examTest);
			} catch (Exception e) {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}

	@ApiOperation("用户搜索考试")
	@RequestMapping(value = "user/examTest/search", method = RequestMethod.GET)
	public Result UserSearchExamTest(@RequestParam("key") String key) {
		return Result.success(examTestService.SearchExamTestWithKey(key, true));
	}

	@ApiOperation("管理员搜索考试")
	@RequestMapping(value = "admin/examTest/search", method = RequestMethod.GET)
	public Result AdminSearchExamTest(@RequestParam("key") String key) {
		return Result.success(examTestService.SearchExamTestWithKey(key, false));
	}

	@ApiOperation("管理员获取考试列表")
	@RequestMapping(value = "admin/examTests/{page}/{count}", method = RequestMethod.GET)
	public Result AdminListExamTest(@PathVariable("page") Integer page, @PathVariable("count") Integer count,
			@RequestParam(required = false,defaultValue = "-1") Integer tag,
			@RequestParam(required = false,defaultValue = "-1") Integer type,
			@RequestParam(required = false,defaultValue = "-1") Integer id) {
		return Result.success(examTestService.ListExamTestWithTagAndType(count, page, type, tag, 0,id));
	}

	@ApiOperation("用户搜索考试列表")
	@RequestMapping(value = "user/examTests/{page}/{count}", method = RequestMethod.GET)
	public Result UserListExamTest(@PathVariable("page") Integer page, @PathVariable("count") Integer count,
			@RequestParam(required = false,defaultValue = "-1") Integer tag, 
			@RequestParam(required = false,defaultValue = "-1") Integer type,
			@RequestParam(required = false,defaultValue = "-1") Integer id) {
		return Result.success(examTestService.ListExamTestWithTagAndType(count, page, type, tag, 1,id));
	}
}
